/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.asn1.lang;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
 

/**
 * A validator class for ASN.1 runtime classes to check constraints etc.
 *
 * @author Thomas Kistel
 * @version $Revision: 763 $
 */
public class Validator {

    /**
     * Checks the SizeContraint of the given field (<code>fieldName</code>) of the class (<code>fieldClass</code>). The given <code>value</code>
     * must match the SizeConstraint, otherwise an <code>java.lang.IllegalArgumentException</code> is thrown.
     * 
     * @param fieldClass the class instance that declares the field (<code>fieldName</code>)
     * @param fieldName the name of the field that declares the SizeConstraint annotation
     * @param value the actual value that should match the SizeConstraint
     * @throws IllegalArgumentException when the <code>value</code> does not match the SizeConstraint
     * @see SizeConstraint
     */
    public static void checkSizeConstraint(Class<?> fieldClass, String fieldName, Integer value) {
        if(value != null) {
            SizeConstraint annotation = getAnnotation(fieldClass, fieldName, SizeConstraint.class);
            if(value > annotation.max()) {
                String fullName = fieldClass.getName() + "." + fieldName;
                throw new IllegalArgumentException("Value " + fullName + " value (" + value + ") is out of size (max=" + annotation.max() + ")");
            }
        }
    }
    
    /**
     * The calls of this method is the same as {@link #checkSizeConstraint(Class, String, Integer)} with
     * <code>value.getNumber()</code>
     */
    public static void checkSizeConstraint(Class<?> fieldClass, String fieldName, NamedNumber value) {
        checkSizeConstraint(fieldClass, fieldName, value.getNumber());
    }
    
    /**
     * Checks the {@link ValueRangeConstraint} of the given field (<code>fieldName</code>) of the class (<code>fieldClass</code>). The given <code>value</code>
     * must match the RangeConstraint, otherwise an <code>java.lang.IllegalArgumentException</code> is thrown.
     * 
     * @param fieldClass the class instance that declares the field (<code>fieldName</code>)
     * @param fieldName the name of the field that declares the RangeConstraint annotation
     * @param value the actual value that should match the RangeConstraint
     * @throws IllegalArgumentException when the <code>value</code> does not match the RangeConstraint
     * @see ValueRangeConstraint
     */
    public static void checkRangeConstraint(Class<?> fieldClass, String fieldName, Integer value) {
        if(value != null) {
            ValueRangeConstraint annotation = getAnnotation(fieldClass, fieldName, ValueRangeConstraint.class);
            if(value < annotation.min() || value > annotation.max()) {
                String fullName = fieldClass.getName() + "." + fieldName;
                throw new IllegalArgumentException("The given " + fullName + " value (" + value + ") is out of range (min=" + annotation.min() + ", max=" + annotation.max() + ")");
            }
        }
    }
    
    /**
     * The calls of this method is the same as {@link #checkRangeConstraint(Class, String, Integer)} with
     * <code>value.getNumber()</code>
     */
    public static void checkRangeConstraint(Class<?> fieldClass, String fieldName, NamedNumber value) {
        checkRangeConstraint(fieldClass, fieldName, value.getNumber());
    }
    
    /**
     * Returns the Annotation of the given <code>annotationClass</code> that is attached to the given field (<code>fieldName</code>) of
     * the given class (<code>fieldClass</code>). This method does not return <code>null</code>.
     * 
     * @param fieldClass the class of the field (<code>fieldName</code>)
     * @param fieldName the name of the field that declares the <code>annotationClass</code>
     * @param annotationClass the Annotation class
     * @return the annotation, never <code>null</code> (instead throws an Error)
     * @throws NoSuchFieldError 
     *              - if the <code>fieldClass</code> does not declare the <code>fieldName</code>
     *              - if the field (<code>fieldName</code>) does not declare the Annotation (<code>annotationClass</code>)
     * @throws IllegalAccessError if the <code>fieldClass</code> cannot access the field <code>fieldName</code>
     */
    public static <A extends Annotation> A getAnnotation(Class<?> fieldClass, String fieldName, Class<A> annotationClass) {
        try {
            Field field = fieldClass.getDeclaredField(fieldName);
            A annotation = field.getAnnotation(annotationClass);
            if(annotation == null) {
                String fullName = fieldClass.getName() + "." + fieldName;
                throw new NoSuchFieldError("The field " + fullName + " does not declare the annotation " + annotationClass);
            }   
            return annotation;
        } catch (NoSuchFieldException e) {
            throw new NoSuchFieldError("The class " + fieldClass.getName() + " does not declare the field " + fieldName);
        } catch (SecurityException e) {
            throw new IllegalAccessError("The class " + fieldClass.getName() + " cannot access the field " + fieldName);
        }
    }
}
